#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/9/7 17:53
# @Author : 闫峰
# @File    : discount_syc.py
import json
import os
import threading
import time

import jsonpath

from common.setting import ensure_path_sep
from testcases.product_category_about.test.three.baseutils import BaseUtils
from testcases.product_category_about.test.three.sku_attribute.test_get_OrderSourceSKUID import TestGetOrderSourceSKUID
from third_services.WalMart.syclisting import SycListing

from utils.logging_tool.log_control import LogHandler
from utils.mysqlUtils.db_pool import Database

current_path = os.path.abspath(__file__)
current_dir = os.path.dirname(current_path)
path_list = current_path.split("\\")
dis_path = "\\".join(path_list[-2:-1])


def remove_file(info_path):
    if os.path.exists(info_path):
        os.remove(info_path)
    else:
        print(f"{info_path} 不存在")


info_path = ensure_path_sep(f"\\logs\\info-{dis_path}{time.time()}")
error_path = ensure_path_sep(f"\\logs\\\error-{dis_path}{time.time()}.log")
warning_path = ensure_path_sep(f"\\logs\\warning-{dis_path}{time.time()}.log")
remove_file(info_path)
remove_file(error_path)
remove_file(warning_path)
INFO = LogHandler(info_path, level='info')
ERROR = LogHandler(error_path, level='error')
WARNING = LogHandler(warning_path, level='warning')


class Test_t_attribute_aliexpress:
    res_ = []

    def discount_syc(self, source_id):
        """
        验证 属性聚合表
        """
        db = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"), "test_jdw_database")
        sql1 = """
        SELECT source_id,source_sku FROM dev_zhcxkj_center_product.t_walmart_listing 
        WHERE id>%s AND id < %s AND source_id=%s AND STATUS=1
        """
        id_offset_start = 0
        id_offset_end = id_offset_start + 10000
        id_max = 4011018
        while True:
            if id_offset_start <= id_max:
                res1 = db.fetch_all_tuple(sql1, (id_offset_start, id_offset_end, source_id))
                if res1:
                    self.handler_data(res1)
                    id_offset_start = id_offset_end - 1
                    id_offset_end = id_offset_start + 10000
                else:
                    id_offset_start = id_offset_end - 1
                    id_offset_end = id_offset_start + 10000
            else:
                break
        return self.res_

    def handler_data(self, res1):
        t = SycListing()
        for data in res1:

            res = t.get_promotion_by_sku(45, data[0], data[1])

            if res:
                if res.get('status') == 'NOT_FOUND':
                    continue
                elif res.get('status') == 'OK':
                    # res = json.dumps(res)
                    currentPriceType = jsonpath.jsonpath(res, "$..currentPriceType")
                    if 'BASE' in currentPriceType:
                        continue
                    else:
                        pricing = jsonpath.jsonpath(res, "$.payload.pricingList.pricing")[0]
                        for p in pricing:
                            currentPrice = p.get('currentPrice').get('value').get('amount')
                            comparisonPrice = p.get('comparisonPrice').get('value').get('amount')
                            effectiveDate = p.get('effectiveDate')
                            expirationDate = p.get('expirationDate')
                            promoId = p.get('promoId')
                            self.res_.append((data[0], data[1], currentPriceType, currentPrice, comparisonPrice,
                                              effectiveDate, expirationDate, promoId))



            else:
                continue


if __name__ == '__main__':
    print(Test_t_attribute_aliexpress().discount_syc(11623))
